const path = require('path')
const fs = require('fs')
const toml = require('toml')

let content = ''

try {
    content = fs.readFileSync(path.resolve(__dirname, '../.toml'), 'utf-8')
} catch (err) {
    console.error(err.message)
}

const config = toml.parse(content)

// console.log(config)

for (const key in config.envs) {
    if (process.env[key] === undefined) {
        process.env[key] = String(config.envs[key])
    }
}

const port = Number(process.env.PORT || 4000)

module.exports = {
    port: port,
    env: process.env.NODE_ENV || 'dev',
    isPrd () {
        return this.env === 'prd'
    },
    isTest () {
        return this.env === 'test'
    },
    db: {
        connectionLimit: 10,
        host: config.db.DB_HOST || 'localhost',
        port: config.db.DB_PORT || '3306',
        database: config.db.DB_NAME,
        user: config.db.DB_USER || 'root',
        password: config.db.DB_PASS || '',
        charset: 'utf8mb4_general_ci'
    }
}
